      subroutine Residual
	  use global
      Implicit none
	  integer :: i,ie,nelt,kloc_e(2)
	  real(8) ::  vcor_e(2),L_e,F1_2(2,3),Flux_e(2,3),U1_2(2,3),U_Temp(2,3),Re(2,3)
	  nelt=nnt-1
	  Resid=0.D0
	  !write(*,*) Resid(1,2),Resid(nnt,2)
	  do ie=1,nelt
	    kloc_e=[ie,ie+1]
	    vcor_e=vcor(kloc_e)
	    L_e=Abs(vcor_e(2)-vcor_e(1))
		
	    U1_2=0.D0
	    Flux_e=F(kloc_e,:)
	    U_Temp(1,:)=-0.5D0*Delta_t*(Flux_e(2,:)-Flux_e(1,:))/L_e
	    U_Temp(2,:)=U_Temp(1,:)
	    U1_2=U(kloc_e,:)+U_Temp
		Call Flux(U1_2,F1_2,2)
		
	    Re(1,:)=-0.5D0*Delta_t*(F1_2(1,:)+F1_2(2,:))
	    Re(2,:)=0.5D0*Delta_t*(F1_2(1,:)+F1_2(2,:))
	    Resid(kloc_e,:)=Resid(kloc_e,:)+Re
	    if (ie==1) then
		  !write(*,*) Resid(1,2)
	      Resid(1,:)=Resid(1,:)+Delta_t*F1_2(1,:)
		  !write(*,*) Resid(1,2)
	    endif
	    if (ie==nelt) then
		  !write(*,*) Resid(nnt,2)
	      Resid(nnt,:)=Resid(nnt,:)-Delta_t*F1_2(2,:)
		  !write(*,*) Resid(nnt,2)
	    endif
		
	  enddo

		   !do i=1,nnt
		   !write(*,*) Resid(i,2)
		   !enddo


      end subroutine